package org.example.service;

import org.example.mapper.StockMapper;
import org.example.polo.Stock;
import org.example.service.i.IStockService;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.ReentrantLock;

@Service
public class StockJavaService implements IStockService {

    private Stock stock = new Stock();

    private ReentrantLock lock = new ReentrantLock();

    public String deduct() {
        stock.setCount(stock.getCount() - 1);
        return "deduct success ,current stock is " + stock.getCount();
    }


    public synchronized String deductSyn() {
        stock.setCount(stock.getCount() - 1);
        return "deduct success ,current stock is " + stock.getCount();
    }

    public synchronized String deductLock() {
        lock.lock();
        try {
            stock.setCount(stock.getCount() - 1);
        }finally {
            lock.unlock();
        }
        return "deduct success ,current stock is " + stock.getCount();
    }
//
    public String clear() {
        stock.setCount(Stock.INIT);

        return "clear success ,current stock is " + stock.getCount();
    }


}
